from api.commands import command_interface
from api.commands.interface import ReturnData, SendType
from basic.entiys.point.PointGetter import PointGetter
from framework.Singleton import Singleton


class UserSite(metaclass=Singleton):
    def __init__(self):
        self.user_site = []  # 这里存放着签到完毕且没有取消签到的用户
        self.gaming = False


class JoinHomeHandler(command_interface, ):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def run(self, *args, **kwargs) -> ReturnData:
        user_site = UserSite().user_site

        print(f"【签到机制执行】: 参数{self.command_dispatch.command_args}")
        __serable_queen = self.serable_queen.get_UserManger()
        all_user = __serable_queen.get_all_user()
        user = self.command_dispatch.command_args[0]

        # 第一个参数是用户的token
        def inner():
            nonlocal user_site

            if len(user_site) == len(all_user):
                a = UserSite()
                a.user_site = []  # 用户满了之后就清空状态
                a.gaming = True  # 开始游戏

                return ReturnData(messgae_mod="JoinHomeHandler", send_type=SendType.send_all, messgae={
                    "status": "full",
                    "nowuser": user_site, "alluser": all_user

                })

            return ReturnData(messgae_mod="JoinHomeHandler", send_type=SendType.send_all, messgae={
                "status": "no_full",
                "nowuser": user_site,
                "alluser": all_user
            })

        if len(self.command_dispatch.command_args) > 1:
            active = self.command_dispatch.command_args[1]
        else:
            active = "in"  # more是 签到
        if active == "in":
            # 签到机制
            print("签到机制")
            if (user in all_user):
                # 如果该用户是注册了势力的
                try:
                    if (user in user_site):  # 已经签到
                        raise AttributeError
                except AttributeError:
                    return ReturnData(messgae_mod="JoinHomeHandler", send_type=SendType.send_self, messgae={
                        "status": "error",
                        "error": "已经签到完毕了"
                    })

                # 筛选用户是否已经签到了
                print("qiandao")
                user_site.append(user)
            return inner()

        elif active == "out":
            # 签离机制
            try:
                user_site.remove(user)
            except:
                return ReturnData(messgae_mod="JoinHomeHandler", send_type=SendType.send_self, messgae={
                    "status": "error",
                    "nowuser": "还没有签到"
                })
            return ReturnData(messgae_mod="JoinHomeHandler", send_type=SendType.send_all, messgae={
                "status": "no_full",
                "nowuser": user_site, "alluser": all_user

            })
        elif active == "only_get":
            # 只是获取当前的状态
            return inner()


class GetMapHandler(command_interface ):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def run(self, *args, **kwargs):
        args = self.command_dispatch.command_args

        if args == [] or args == ["self"]:
            send_policy = SendType.send_self
        else:
            send_policy = SendType.send_all

        if UserSite().gaming:
            web_json = PointGetter.apdot_to_web_json()
            return ReturnData(
                "MapUpdate",
                send_policy,
                messgae={
                    "data": web_json
                }
            )
        else:
            return ReturnData(
                "Empty",
                send_policy,
                messgae={}
            )
